package com.dingshi.security.view;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dingshi.security.R;

public class MainClockView extends FrameLayout {

	public ImageView clock_weather_icon_panel;
	public TextView clock_am_panel, clock_time_panel, clock_data_panel,
			clock_week_panel, clock_lunar_panel, clock_weather_panel,
			clock_temperature_panel, clock_city_panel;

	private LayoutInflater mInflater;
	private FrameLayout allLayout;

	private FrameLayout mBarView;
	public static boolean isRungingtime = false;;
	public Timer timetask;
	Handler cwjHandler = new Handler();

	public MainClockView(Context context) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (FrameLayout) mInflater.inflate(R.layout.security_clock,
				null);
		addView(mBarView);
		allLayout = (FrameLayout) mBarView.findViewById(R.id.clock_panel);
		clock_weather_icon_panel = (ImageView) mBarView
				.findViewById(R.id.clock_weather_icon_panel);
		clock_am_panel = (TextView) mBarView.findViewById(R.id.clock_am_panel);
		clock_time_panel = (TextView) mBarView
				.findViewById(R.id.clock_time_panel);
		clock_data_panel = (TextView) mBarView
				.findViewById(R.id.clock_data_panel);
		clock_week_panel = (TextView) mBarView
				.findViewById(R.id.clock_week_panel);
		// clock_lunar_panel = (TextView) mBarView
		// .findViewById(R.id.clock_lunar_panel);
		clock_weather_panel = (TextView) mBarView
				.findViewById(R.id.clock_weather_panel);
		clock_temperature_panel = (TextView) mBarView
				.findViewById(R.id.clock_temperature_panel);
		clock_city_panel = (TextView) mBarView
				.findViewById(R.id.clock_city_panel);

	}

	public MainClockView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (FrameLayout) mInflater.inflate(R.layout.security_clock,
				null);
		addView(mBarView);
		allLayout = (FrameLayout) mBarView.findViewById(R.id.clock_panel);
		clock_weather_icon_panel = (ImageView) mBarView
				.findViewById(R.id.clock_weather_icon_panel);
		clock_am_panel = (TextView) mBarView.findViewById(R.id.clock_am_panel);
		clock_time_panel = (TextView) mBarView
				.findViewById(R.id.clock_time_panel);
		clock_data_panel = (TextView) mBarView
				.findViewById(R.id.clock_data_panel);
		clock_week_panel = (TextView) mBarView
				.findViewById(R.id.clock_week_panel);
		// clock_lunar_panel = (TextView) mBarView
		// .findViewById(R.id.clock_lunar_panel);
		clock_weather_panel = (TextView) mBarView
				.findViewById(R.id.clock_weather_panel);
		clock_temperature_panel = (TextView) mBarView
				.findViewById(R.id.clock_temperature_panel);
		clock_city_panel = (TextView) mBarView
				.findViewById(R.id.clock_city_panel);
	}

	public void setTime(String time) {
		clock_time_panel.setText(time);
	}

	public void setAmOrPm(String amorpm) {
		clock_am_panel.setText(amorpm);
	}

	public void setDate(String data) {
		clock_data_panel.setText(data);
	}

	public void setWeek(String week) {
		clock_week_panel.setText(week);
	}

	public void setWeatherIcon(int icon) {
		clock_weather_icon_panel.setImageResource(icon);
	}

	public void setWeather(String weather) {
		clock_weather_panel.setText(weather);
	}

	public void setTemperature(String temperature) {
		clock_temperature_panel.setText(temperature);
	}

	static int x = 0;

	public void timeStop() {
		if (timetask != null) {
			timetask.cancel();
			timetask = null;
		}
	}

	public void timeStart() {

		timetask = new Timer();
		timetask.schedule(new TimerTask() {

			@Override
			public void run() {

				Calendar ca = Calendar.getInstance();
				final int year = ca.get(Calendar.YEAR);// 获取年份
				final int month = ca.get(Calendar.MONTH);// 获取月份
				final int day = ca.get(Calendar.DATE);// 获取日
				final int minute = ca.get(Calendar.MINUTE);// 分
				final int hour = ca.get(Calendar.HOUR);// 小时
				final int second = ca.get(Calendar.SECOND);// 秒
				final int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
				final int amOrPm = ca.get(Calendar.AM_PM);

				cwjHandler.post(new Runnable() {

					@Override
					public void run() {

						clock_am_panel.setText(amOrPm == 0 ? "上午" : "下午");

						clock_time_panel.setText((hour < 10 ? " 0" + hour : " "
								+ hour)
								+ ":"
								+ (minute < 10 ? " 0" + minute + " " : " "
										+ minute + " "));
						clock_data_panel.setText((month + 1) + "月" + day + "日");
						clock_week_panel.setText("星期" + (WeekOfYear - 1));

					}
				});

			}
		}, 1000, 1000);

	}
}
